
package com.yimixian.app.user;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.activity.BaseNaviFragmentGroupActivity;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.User;
import com.yimixian.app.rest.YmxDataService;
import com.yimixian.app.ui.ProgressDialogFragment;
import com.yimixian.app.util.UiUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * 修改用户昵称页面 Created by liuletao on 2015/8/21.
 */
public class EditNickActivity extends BaseNaviFragmentGroupActivity implements View.OnClickListener {

    private EditText editNick;
    private ImageView imgClear;
    private User user_local;
    private User user;
    private String nickTv = "";
    private YmxDataService mYmxDataService;
    private DataManager mDataManager;
    private ProgressDialogFragment mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_nick);
        mYmxDataService = YmxDataService.getInstance();
        mDataManager = DataManager.getInstance();
        user_local = mDataManager.get(CacheKeys.CURRENT_USER);
        user = new User();
        user.age = user_local.age;
        user.nickName = user_local.nickName;
        user.sex = user_local.sex;
        user.careerMap = user_local.careerMap;
        user.career = user_local.career;
        setTopNavi(R.drawable.ic_back_selector, getString(R.string.personal_center_save),
                getString(R.string.personal_center_nick), this, null);
        editNick = (EditText) findViewById(R.id.edit_nick);
        imgClear = (ImageView) findViewById(R.id.img_clear);
        imgClear.setOnClickListener(this);
        nickTv = user_local == null ? "" : user_local.nickName;
        imgClear.setVisibility(StringUtils.isEmpty(nickTv) ? View.INVISIBLE : View.VISIBLE);
        editNick.setText(nickTv);
        editNick.setSelection(nickTv.length());
        editNick.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                imgClear.setVisibility(StringUtils.isEmpty(charSequence.toString()) ? View.INVISIBLE
                        : View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    public static void buildIntent(Context context, User user) {
        Intent intent = new Intent(context, EditNickActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_bar_right_text:
                String str_nick = editNick.getText().toString();
                if (str_nick.trim().length() == 0) {
                    UiUtils.showToast(this,"昵称不能为空");
                    return;
                }
                user.nickName = str_nick;
                updateUserInfo();
                break;
            case R.id.img_clear:
                editNick.setText("");
                break;
            default:
                break;
        }
    }

    /**
     * 更新用户信息
     */
    public void updateUserInfo() {
        mProgressDialog = UiUtils.showProgressDialog(this, "请稍后...", true);
        mYmxDataService.updateUserInfo(user).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<User>() {
                    @Override
                    public void call(User user) {
                        if (user == null)
                            return;
                        mDataManager.put(CacheKeys.CURRENT_USER, user);
                        finish();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        UiUtils.dismiss(mProgressDialog);
                        UiUtils.toastError(EditNickActivity.this, throwable);
                    }
                });
    }
}
